package cn.tedu.charging.order.rabbit;

import cn.tedu.charging.order.common.OrderStatusConstants;
import cn.tedu.charging.order.config.RabbitMQConfiguration;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import cn.tedu.charging.order.pojo.po.OrderMQPO;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RabbitMQConsumer {

    @Autowired
    private OrderSuccessMapper orderSuccessMapper;

    //消费者不去消费 正常的队列  charging_order_queue
    //@RabbitListener(queues = RabbitMQConfiguration.QUEUE_NAME)
    //消费者消费 死信队列
    @RabbitListener(queues = RabbitMQConfiguration.DEAD_LETTER_QUEUE)
    public void consumeMessage(OrderMQPO orderMQPO) {
        log.info("消费者接收到消息：{}",orderMQPO);
        //消费到消息后，查询成功订单记录
        //判断订单状态
        //  充电中   表示超时 修改订单的状态 为 异常结束
        //  充电完成 无需处理
        //更新目标的值 bill_status = 异常结束
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillStatus(OrderStatusConstants.ORDER_STATUS_EXCEPTION_END);
        //更新的条件
        UpdateWrapper updateWrapper = new UpdateWrapper();
        //条件1 bill_id = message
        updateWrapper.eq("bill_id",orderMQPO.getOrderNo());
        //条件2 bill_status = 充电中
        updateWrapper.eq("bill_status",OrderStatusConstants.ORDER_STATUS_PROCESS);
        //update charging_bill_success set bill_status = 异常结束
        // where bill_id = message and bill_status = 充电中
        orderSuccessMapper.update(chargingBillSuccessPO,updateWrapper);
    }
}
